<template>
  <div>
    <div class="forgot-panel">
      <el-button type="text" class="page-back" @click="pageBack">
        <i class="fa fa-angle-double-left" aria-hidden="true"></i> {{ $root.lang.forgot.back }}
      </el-button>
      <el-form @submit.native.prevent class="forgot-form" ref="form" :model="form" :rules="formRules" label-width="0px">
        <el-form-item prop="code">
          <el-input type="text" v-model="form.code" @keyup.enter.native="onSubmit" :placeholder="$root.lang.forgotStep2.code"></el-input>
          <p class="form-item-tip">{{ $root.lang.forgotStep2.codeTip }}</p>
        </el-form-item>
        <el-form-item>
          <p class="form-error">{{ errmsg }}</p>
          <el-button class="forgot-btn" type="primary" @click="onSubmit" :loading="loading">{{ $root.lang.forgotStep2.btn }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      form: {
        code: '',
      },
      formRules: {
        code: [{ required: true, message: this.$root.lang.forgotStep2.rule.code, trigger: 'blur' }],
      },
      errmsg: '',
      infoMsg: this.info
    }
  },
  props: {
    info: {
      type: Object,
      default: {}
    }
  },
  methods: {
    pageBack() {
      this.$emit('receive',{
        key: 'activeIndex',
        value: 1
      })
    },
  	onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.checkStep2()
        } else {
          return false
        }
      })
  	},
    checkStep2() {
      this.loading = true
      this.request({
        url: '/api/user/forget/password/check/code',
        method: 'post',
        data: {
          updateRecordNumber: this.infoMsg.updateRecordNumber,
          code: this.form.code
        }
      }).then(response => {
        this.loading = false
        this.$emit('receive',{
          key: 'activeIndex',
          value: 3
        })
        this.$emit('twoReceiveMsg',response.result.updateRecordNumber)
      }).catch(error => {
        this.loading = false
        this.errmsg = error.errmsg
      })
    }
  },
  created() {
    console.log(this.infoMsg)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import '@/css/style.scss';
.forgot-panel {
  position: relative;
  margin: 30px auto 0;
  padding: 100px 50px;
  width: 600px;
  background-color: #ffffff;
}
.forgot-form {
  margin: 0 auto;
  width: 60%;
}
.el-form-item {
  margin-bottom: 20px;
}
.forgot-btn {
  width: 100%;
}
</style>
